// 安全工具函数
export const SecurityUtils = {
    // 生成动态token
    generateDynamicToken() {
        return btoa(Date.now() + Math.random().toString(36).substr(2));// 生成动态token
    },

    // 检测调试模式
    detectDebugMode() {
        const threshold = 100;
        let start = performance.now();
        debugger;
        let end = performance.now();
        return end - start > threshold;
    },

    // 检测开发者工具
    detectDevTools() {
        // 方法1: 检测控制台对象是否存在
        if (!window.console || !window.console.log) {
            return false;
        }

        // 方法2: 检测控制台是否被重写（开发者工具打开时console会被重写）
        const detectionObject = {};
        detectionObject.toString = function () {
            this.isDetected = true;
            return 'devtools-detection';
        };
        console.log('%c', detectionObject);

        // 方法3: 检测控制台大小（开发者工具打开时窗口大小会变化）
        const widthThreshold = window.outerWidth - window.innerWidth > 160;
        const heightThreshold = window.outerHeight - window.innerHeight > 160;
        return detectionObject.isDetected || widthThreshold || heightThreshold;
    },

    // 视频URL加密
    encryptVideoUrl(url, key = 'video-protection-key') {
        let result = '';
        for (let i = 0; i < url.length; i++) {
            result += String.fromCharCode(url.charCodeAt(i) ^ key.charCodeAt(i % key.length));
        }
        return btoa(result);
    },

    // 视频URL解密
    decryptVideoUrl(encryptedUrl, key = 'video-protection-key') {
        try {
            const decoded = atob(encryptedUrl);
            let result = '';
            for (let i = 0; i < decoded.length; i++) {
                result += String.fromCharCode(decoded.charCodeAt(i) ^ key.charCodeAt(i % key.length));
            }
            return result;
        } catch (error) {
            return encryptedUrl;
        }
    }
};


// 导出默认实例
export default SecurityUtils;